/*
    Geocache: some utilities for managing and visualizing geocache information
    Copyright (C) 2008  Gary Jackson

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
/*
 * FileCollection.java
 */

package com.thegbomb.common;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;

/**
 *
 * @author Gary Jackson
 */
public class FileCollection implements Collection<File> {
    private Collection<File> backing;
    
    private static final FileFilter dirFilter = new FileFilter() {
        public boolean accept(File pathname) {
            return pathname.isDirectory();
        }
    };
    
    /**
     * Creates a new instance of FileCollection
     */
    public FileCollection(File directory, FileFilter filter, boolean recurse) throws IOException {
        if (! directory.isDirectory())
            throw new IOException("given starting point is not a directory");

        Collection<File> backing = new LinkedList<File>();
        if (recurse) {
            LinkedList<File> directories = new LinkedList<File>();
            directories.add(directory);
            
            while (! directories.isEmpty()) {
                File current = directories.removeFirst();
                backing.addAll(Arrays.asList(current.listFiles(filter)));
                directories.addAll(Arrays.asList(current.listFiles(dirFilter)));
            }
        } else {
            backing.addAll(Arrays.asList(directory.listFiles(filter)));
        }
        
        this.backing = Collections.unmodifiableCollection(backing);
    }
    
    public int size() {
        return backing.size();
    }

    public boolean isEmpty() {
        return backing.isEmpty();
    }

    public boolean contains(Object o) {
        return backing.contains(o);
    }

    public Iterator<File> iterator() {
        return backing.iterator();
    }

    public Object[] toArray() {
        return backing.toArray();
    }

    public <T> T[] toArray(T[] a) {
        return backing.toArray(a);
    }

    public boolean add(File o) {
        throw new UnsupportedOperationException("this collection is immutable");
    }

    public boolean remove(Object o) {
        throw new UnsupportedOperationException("this collection is immutable");
    }

    public boolean containsAll(Collection<?> c) {
        return backing.containsAll(c);
    }

    public boolean addAll(Collection<? extends File> c) {
        throw new UnsupportedOperationException("this collection is immutable");
    }

    public boolean removeAll(Collection<?> c) {
        throw new UnsupportedOperationException("this collection is immutable");
    }

    public boolean retainAll(Collection<?> c) {
        throw new UnsupportedOperationException("this collection is immutable");
    }

    public void clear() {
        throw new UnsupportedOperationException("this collection is immutable");
    }    
}
